package com.yu.mall.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yu.mall.dto.BaseDTO;
import com.yu.mall.entity.ShoppingCart;
import com.yu.mall.exception.ServiceException;
import com.yu.mall.mapper.ShoppingCartMapper;
import com.yu.mall.util.JDUtils;
import com.yu.mall.util.ResultUtils;
import com.yu.mall.vo.ResultVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@RestController
@RequestMapping("shoppingCart")
@Api(tags = "购物车管理",value = "购物车管理")
public class ShoppingCartController {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    
    @PostMapping("addShoppingCart")
    @ApiOperation(value = "添加购物车")
    public ResultVO addShoppingCart(BaseDTO baseDTO,ShoppingCart shoppingCart){
    	try {
    		Example example = new Example(ShoppingCart.class);
			Criteria createCriteria = example.createCriteria();
			if(!"".equals(baseDTO.getOpenid())&& baseDTO.getOpenid() !=null) {
				createCriteria.andEqualTo("openid", baseDTO.getOpenid());
			}else {
				return ResultUtils.error("添加失败");
			}
			if(!"".equals(shoppingCart.getSkuId())&& shoppingCart.getSkuId() !=null) {
				createCriteria.andEqualTo("skuId", shoppingCart.getSkuId());
			}else {
				return ResultUtils.error("添加失败");
			}
			List<ShoppingCart> selectByExample = shoppingCartMapper.selectByExample(example);
			if(selectByExample.size()>0) {
				ShoppingCart shoppingCart2 = selectByExample.get(0);
				shoppingCart2.setNum(shoppingCart2.getNum()+shoppingCart.getNum());
				shoppingCartMapper.updateByPrimaryKey(shoppingCart2);
			}else {
				shoppingCart.setOpenid(baseDTO.getOpenid());
	    		shoppingCart.setCreateDatetime(new Date());
	    		int insert = shoppingCartMapper.insert(shoppingCart);
			}
    		return ResultUtils.success("添加成功");
		} catch (Exception e) {
			// TODO: handle exception
			return ResultUtils.error("添加失败");
		}
    }
    
    @GetMapping("queryShoppingCartList")
    @ApiOperation(value = "查询购物车")
    @ApiImplicitParam(name = "id",value="多个用,分割",required=false)
    public ResultVO queryShoppingCartList(BaseDTO baseDTO,String id){
    	try {
    		Example example = new Example(ShoppingCart.class);
			Criteria createCriteria = example.createCriteria();
			if(!"".equals(baseDTO.getOpenid())&& baseDTO.getOpenid() !=null) {
				createCriteria.andEqualTo("openid", baseDTO.getOpenid());
				if(!"".equals(id)&& id!=null) {
					String[] split = id.split(",");
					createCriteria.andIn("id", Arrays.asList(split));
				}
	    		List<ShoppingCart> selectByExample = shoppingCartMapper.selectByExample(example);
	    		for (ShoppingCart shoppingCart : selectByExample) {
	    			try {
	    				Map<String, Object> priceMap = JDUtils.priceGetSellPrice(shoppingCart.getSkuId());
		                if (!priceMap.isEmpty()) {
		                    JSONObject priceJson = ((JSONArray) priceMap.get("data")).getJSONObject(0);
		                    shoppingCart.setJdPrice(priceJson.getBigDecimal("jdPrice"));
		                }
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
					}
				}
	    		return ResultUtils.success(selectByExample);
			}else {
				return ResultUtils.error("获取失败");
			}
		} catch (Exception e) {
			// TODO: handle exception
			return ResultUtils.error("获取失败");
		}
    }
    
    @PostMapping("updateShoppingCart")
    @ApiOperation(value = "修改购物车数量")
    public ResultVO updateShoppingCart(ShoppingCart shoppingCart){
    	try {
    		shoppingCartMapper.updateByPrimaryKeySelective(shoppingCart);
    		return ResultUtils.success();
		} catch (Exception e) {
			// TODO: handle exception
			return ResultUtils.error("修改失败");
		}
    }
    
    @PostMapping("deleteShoppingCart")
    @ApiOperation(value = "删除购物车")
    public ResultVO deleteShoppingCart(ShoppingCart shoppingCart){
    	try {
    		int insert = shoppingCartMapper.delete(shoppingCart);
    		return ResultUtils.success();
		} catch (Exception e) {
			// TODO: handle exception
			return ResultUtils.error("删除失败");
		}
    }
    
}
